/* 
 * @ProjectName VideoGoJar
 * @Copyright HangZhou Hikvision System Technology Co.,Ltd. All Right Reserved
 * 
 * @FileName CameraListActivity.java
 * @Description 这里对文件进行描述
 * 
 * @author xia xingsuo
 * @data 2015-11-5
 * 
 * @note 这里写本文件的详细功能描述和注释
 * @note 历史记录
 * 
 * @warning 这里写本文件的相关警告
 */
package com.videogo.ui.cameralist;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.videogo.exception.BaseException;
import com.videogo.open.R;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.EZPlayer;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;
import com.videogo.realplay.RealPlayStatus;
import com.videogo.universalimageloader.core.ImageLoader;
import com.videogo.util.ConnectionDetector;
import com.videogo.util.LogUtil;
import com.videogo.util.Utils;
import com.videogo.widget.loading.LoadingTextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static android.view.SurfaceHolder.*;

/**
 * 摄像头 gridview adapter
 */
public class EZCameraGridAdapter extends ArrayAdapter<EZCameraInfo> {
    private Context mContext = null;
    public String TAG = "EZCameraGridAdapter";
    //***屏幕当前方向*/
    private EZOpenSDK mEZOpenSDK = EZOpenSDK.getInstance();
    private Handler mHandler = null;
    public EZCameraGridAdapter(Context context, List<EZCameraInfo> list) {
        super(context, 0, list);
        mContext = context;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_video, null);
        RelativeLayout mRealPlayLoadingRl = (RelativeLayout) convertView.findViewById(R.id.realplay_loading_rl);
        TextView mRealPlayTipTv = (TextView) convertView.findViewById(R.id.realplay_tip_tv);
        ImageView mRealPlayPlayIv = (ImageView) convertView.findViewById(R.id.realplay_play_iv);
        LoadingTextView mRealPlayPlayLoading = (LoadingTextView) convertView.findViewById(R.id.realplay_loading);
        LinearLayout mRealPlayPlayPrivacyLy = (LinearLayout) convertView.findViewById(R.id.realplay_privacy_ly);
        EZCameraInfo mCameraInfo = getItem(i);
        mHandler = new Handler(new Handler.Callback(){
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_SUCCESS:
//                        handlePlaySuccess(msg);
                        break;
                    case EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_FAIL:
//                        handlePlayFail(msg.arg1, msg.arg2);
                        break;


                }    return false;
            }
        });
        Log.e(TAG,mCameraInfo.getOnlineStatus()+"");
        mRealPlayLoadingRl.setVisibility(View.GONE);
        SurfaceView mRealPlaySv = (SurfaceView) convertView.findViewById(R.id.realplay_sv);
        mRealPlaySv.setVisibility(View.VISIBLE);
        SurfaceHolder mRealPlaySh = null;
        EZPlayer mEZPlayer = null;
        mRealPlaySv.getHolder().addCallback(new MyCallback(mEZPlayer, mRealPlaySh));
        startRealPlay(0, mCameraInfo, mRealPlaySh);
        return convertView;
    }

    public class MyCallback implements Callback {
        public EZPlayer mEZPlayer;
        public SurfaceHolder mRealPlaySh;

        MyCallback(EZPlayer mEZPlayer, SurfaceHolder mRealPlaySh) {
            this.mEZPlayer = mEZPlayer;
            this.mRealPlaySh = mRealPlaySh;
        }

        public void surfaceCreated(SurfaceHolder holder) {
            if (mEZPlayer != null) {
                mEZPlayer.setSurfaceHold(holder);
            }
            mRealPlaySh = holder;
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            if (mEZPlayer != null) {
                mEZPlayer.setSurfaceHold(null);
            }
            mRealPlaySh = null;
        }

        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

        }
    }


    /**
     * 开始播放
     *
     * @see
     * @since V2.0
     */
    private void startRealPlay(int mStatus, EZCameraInfo mCameraInfo, final SurfaceHolder mRealPlaySh) {
        // 增加手机客户端操作信息记录
        LogUtil.debugLog(TAG, "startRealPlay");
        if (mStatus == RealPlayStatus.STATUS_START || mStatus == RealPlayStatus.STATUS_PLAY) {
            return;
        }
        // 检查网络是否可用
        if (!ConnectionDetector.isNetworkAvailable(mContext)) {
//            setRealPlayFailUI(getString(R.string.realplay_play_fail_becauseof_network));
            return;
        }
        mStatus = RealPlayStatus.STATUS_START;
        if (mCameraInfo != null) {
            final String cameraId = Utils.getCameraId(mCameraInfo.getCameraId());
            final EZPlayer mEZPlayer = mEZOpenSDK.createPlayer(mContext, cameraId);
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    if (mEZPlayer == null)
                        return;
//                    if (mDeviceInfo == null) {
//                        try {
//                            mDeviceInfo = mEZOpenSDK.getDeviceInfo(cameraId);
//                        } catch (BaseException e) {
//                            e.printStackTrace();
//                            return;
//                        }
//                    }
                    mEZPlayer.setHandler(mHandler);
                    mEZPlayer.setSurfaceHold(mRealPlaySh);
                    mEZPlayer.startRealPlay();
                }
            };
            Thread thr = new Thread(run);
            thr.start();

        }
    }
}